package com.lewis.system.controller;

import com.lewis.core.base.domain.BaseResult;
import com.lewis.core.base.domain.entity.SysDictData;
import com.lewis.core.base.domain.entity.SysMenu;
import com.lewis.core.base.domain.entity.SysUser;
import com.lewis.core.base.domain.model.LoginBody;
import com.lewis.core.base.domain.model.LoginUser;
import com.lewis.core.constant.Constants;
import com.lewis.core.utils.MapUtil;
import com.lewis.core.utils.ServletUtil;
import com.lewis.system.service.ISysConfigService;
import com.lewis.system.service.ISysDictDataService;
import com.lewis.system.service.ISysMenuService;
import com.lewis.system.web.service.SysLoginService;
import com.lewis.system.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 登录验证
 *
 * @author Lewis
 */
@Api(tags = "登录验证")
@RestController
public class SysLoginController {

    @Resource
    private SysLoginService sysLoginService;

    @Resource
    private ISysMenuService sysMenuService;

    @Resource
    private TokenService tokenService;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private ISysDictDataService sysDictDataService;

    @ApiOperation(value = "登录方法", notes = "登录方法")
    @PostMapping("/login")
    public Object login(@RequestBody LoginBody loginBody) {
        BaseResult ok = BaseResult.ok();
        Map<String, Object> resultMap = MapUtil.object2MapByReflect(ok);
        // 生成令牌
        String token = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        resultMap.put(Constants.TOKEN, token);
        return resultMap;
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("getInfo")
    public Object getInfo() {

        BaseResult ok = BaseResult.ok();
        Map<String, Object> resultMap = MapUtil.object2MapByReflect(ok);

        LoginUser loginUser = tokenService.getLoginUser(ServletUtil.getRequest());
        SysUser user = loginUser.getUser();

        // 看板娘链接
        String kbnLinkCode = sysConfigService.selectConfigByKey("kbn.link.code");
        if (!"".equals(kbnLinkCode)) {
            SysDictData sysDictData = sysDictDataService.getOneByDictTypeAndDictValue("kbn_link", kbnLinkCode);
            if (sysDictData != null) {
                resultMap.put("kbnLink", sysDictData.getRemark());
            }
        }

        resultMap.put("user" , user);

        return resultMap;
    }

    @ApiOperation(value = "获取路由信息", notes = "获取路由信息")
    @GetMapping("getRouters")
    public BaseResult getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtil.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(user.getId());
        return BaseResult.ok().setData(sysMenuService.buildMenus(menus));
    }
}
